#version 460
#extension GL_GOOGLE_include_directive : enable

#include "../../../../core/shader_binding_ID.h"
#include "../../../../core/shader_taster_transform.h"
#include "../../../../core/shader_taster_color.h"
#include "../../../../core/shader_taster_line.h"



layout(std140, set=0, binding=2) uniform U3_Matrix {
	vec4 mat_LightDir[4];	
};


//蒙皮权重 [骨骼数量][骨骼内存偏移]
layout(std430, column_major, binding = DEF_BINDING_Bones_Props) readonly buffer U7_VertexSkeleton {
	uvec2 m_BonesProp[];
};

layout(std430, column_major, binding = DEF_BINDING_Bones_Weigth) readonly buffer U_VertexWeigth {
	S_BonesWeigth m_ID_Weigth[];
};

//骨骼对应变换
layout(std430, column_major, binding = DEF_BINDING_Bones_Matrix) readonly buffer U_VertexSkeletonMatrix {
	mat4 m_SkeletonMatrix[];
};



layout(location = 0) in vec3 inPos;
layout(location = 1) in vec3 inNormal;
layout(location = 2) in vec2 inUV;


layout(location = 0) out vec4 vColor;
layout(location = 1) out vec3 vNormal;
layout(location = 2) out vec3 vLightPos[4];
//layout(location = 6) out flat uint vInstanceIndex;
out gl_PerVertex {
	vec4 gl_Position;
};


void main(void){
	//gl_InstanceIndex
	//vInstanceIndex = gl_BaseInstance;
	mat4 bonesMat_som = mat_Model[gl_InstanceIndex + drawProp.MatrixOffset];
	//固定顶点坐标
	vec4 p = bonesMat_som * vec4(inPos, 1.0);
	vec4 a = p;

	//vColor = f_glsl_buildColor(InstanceColor[gl_BaseInstance + drawProp.ColorPropOffset]);
	vColor = f_glsl_buildColor(InstanceColor[gl_InstanceIndex + drawProp.ColorPropOffset]);
	S_MeshTranformProp prop = m_LinesProp[drawProp.MeshPropOffset + gl_DrawID];
	//vColor = f_glsl_buildColor(InstanceColor[gl_BaseInstance]);
	//vColor = vec4(0.5,0.5,0.5,1);

	mat3x3 normalMax = mat3x3(bonesMat_som);
	vNormal = normalize((normalMax * inNormal).xyz);
	vec3 n = vNormal;

	//mat4 bonesMat_A;
	//bonesMat_A[0] = vec4(1,0,0,0);
	//bonesMat_A[1] = vec4(0,1,0,0);
	//bonesMat_A[2] = vec4(0,0,1,0);
	//bonesMat_A[3] = vec4(0,0,0,1);
	if(drawProp.BonesOffset > 0){
		uvec2 bonesID = m_BonesProp[gl_VertexIndex];
		
		for(uint i=0; i<bonesID.x; ++i){
			S_BonesWeigth id_weigth = m_ID_Weigth[bonesID.y + i];
			float weigth = id_weigth.Weigth;
				
			mat4 mat = m_SkeletonMatrix[id_weigth.BonesID];
			
			vec4 q = mat[1];
			//固定顶点到初始骨骼坐标
			vec3 p_pos = p.xyz - mat[3].xyz;
			//骨骼缩放顶点
			p_pos *= mat[0].xyz;
			//newPos.x = p.w * q.x + q.w * p.x + p.y * q.z - q.y * p.z;
			//newPos.y = p.w * q.y + q.w * p.y + p.z * q.x - q.z * p.x;
			//newPos.z = p.w * q.z + q.w * p.z + p.x * q.y - q.x * p.y;
			//newPos.w = q.w * q.w - q.x * q.x - q.y * q.y - q.z * q.z;
			//vec3 newPos = p_pos*(2.0f*q.w*q.w-1.0f) + cross(q.xyz, p_pos)*q.w*2.0f + (q.xyz)*dot((q.xyz), p_pos)*2.0f;
			vec3 newPos = p_pos*(2.0f*q.w*q.w-1.0f) - cross(q.xyz, p_pos)*q.w*2.0f + q.xyz*dot(q.xyz, p_pos)*2.0f;
			vec3 newNormal = vNormal*(2.0f*q.w*q.w-1.0f) - cross(q.xyz, vNormal)*q.w*2.0f + q.xyz*dot(q.xyz, vNormal)*2.0f;
			
			//旋转后的固定顶点加上变换后骨骼坐标
			newPos += mat[2].xyz;
			a.xyz = mix(a.xyz, newPos, weigth);
			n = mix(n, newNormal, weigth);
			//p.xyz = newPos;
			//break;
			

			if(i == 3){
				vColor = vec4(1,0,0, 1);
			}
			if(i == 4){
				vColor = vec4(0,1,0, 1);
			}
			if(i == 5){
				vColor = vec4(0,0,1, 1);
			}
			if(i == 6){
				vColor = vec4(1,1,0, 1);
				break;
			}
		}
	}
	
	//float weigth = m_VertexWeigth[10 * m_VertexNum + gl_VertexIndex];
	//float weigth = m_VertexWeigth[gl_VertexIndex];
	//vColor = vec3(weigth, 0, 0);
	p = a;
	vNormal = n;
	gl_Position = mat_ProjecView[0] * p;
	//gl_Position = vec4(inPos, 1.0);
	//vColor = inColor;
	
	//vNormal = normalize(inNormal);
	vLightPos[0] = normalize(inverse(mat3(mat_ProjecView[1])) * vec3(0.0, 0.0, -1.0));
	

	
	switch(prop.m_Activate){
		case 1:
			//p.xyz -= vLightPos[1] * 0.01;
			gl_Position.z += 0.1 / length(gl_Position.xyz - vLightPos[0]);
			//gl_Position.z += 0.001;
		break;

	default:
		//gl_Position.y += 0.1;
		break;
	}

	
	vLightPos[1] = normalize(vLightPos[0]);
	vLightPos[2] = normalize(vec3(1.0, 1.0, 0.0));
}


 